package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fz.entity.Permission;
import com.fz.mapper.PermissionMapper;
import com.fz.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 存储系统权限信息 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Resource
    PermissionMapper tPermissionMapper;

    @Override
    public List<Permission> getList() {
        return getByParentId(0L);
    }

    private List<Permission> getByParentId(Long parentId) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        List<Permission>list = this.list(queryWrapper);
        for(Permission tPermission : list) {
            List<Permission>children = this.getByParentId(tPermission.getId());
            tPermission.setChildren(children);
        }
        return list;
    }

    @Override
    public Boolean add(String name, String description, String path, Integer level,Integer parentId, Integer type) {
        return this.save(new Permission(name,description,path,level,Long.valueOf(parentId),type));
    }

    @Override
    public Boolean update(Long id, String name, String description, String path, Integer level,Integer parentId, Integer type) {
        return this.updateById(new Permission(id,name,description,path,level,Long.valueOf(parentId),type));
    }

    @Override
    public Boolean del(Long id) {
        return this.removeById(id);
    }

    @Override
    public List<Permission> getByUserId(Long userId) {
        return tPermissionMapper.getByUserId(userId);
    }

}
